package com.example.videorecordtest;

import androidx.appcompat.app.AppCompatActivity;

import android.hardware.Camera;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Handler;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.TextView;

import me.zhanghai.android.materialprogressbar.MaterialProgressBar;


public class VideoRecordActivity extends AppCompatActivity {
    public static final int TYPE_VIDEO=0;//视频模式
    public static final int TYPE_IMAGE=1;//拍照模式

    private final String TAG="VideoRecordActivity";
    private boolean mStartedFlag;//录像中标志
    private boolean mPlayFlag;
    private MediaRecorder mRecorder;
    private SurfaceHolder mSurfaceHolder;
    private Camera mCamera;
    private MediaPlayer mMediaPlayer;
    private String dirPath;//目标文件夹地址
    private String path;//最终视频路径
    private String imgPath;//缩略图 或 拍照模式图片位置
    private int timer=0;//计时器
    private final int maxSec = 15;//视频总时长
    private long startTime=0L;//起始时间毫秒
    private long stopTime=0L;//结束时间毫秒
    private boolean cameraReleaseEnable = true;//回收摄像头
    private boolean recorderReleaseEnable;//回收recorder
    private boolean playerReleaseEnable;//回收palyer

    private MaterialProgressBar mProgress;
    private SurfaceView surfaceView;
    private Button mBtnPlay,mBtnRecord,mBtnCancle,mBtnSubmit;
    private TextView mTvRecordTip;
    private int mType=TYPE_VIDEO;//默认为视频模式
    //用于记录视频录制时长
    private Handler handler=new Handler();
    private Runnable runnable=new Runnable() {
        @Override
        public void run() {
           timer++;
           if (timer<100){
               // 之所以这里是100 是为了方便使用进度条
               mProgress.setProgress(timer);
               //之所以每一百毫秒增加一次计时器是因为：总时长的毫秒数 / 100 即每次间隔延时的毫秒数 为 100
               handler.postDelayed(this, maxSec * 10L);
           }else {
               //停止录制 保存录制的流、显示供操作的ui
               //stopRecord();
               System.currentTimeMillis();
           }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_record);
        initView();


    }

    private void initView() {
        mMediaPlayer=new MediaPlayer();
        //mCamera= new Camera();
        surfaceView=findViewById(R.id.mSurfaceview);
        mTvRecordTip=findViewById(R.id.mTvRecordTip);
        mBtnRecord=findViewById(R.id.mBtnRecord);
        mBtnPlay=findViewById(R.id.mBtnPlay);
        mBtnCancle=findViewById(R.id.mBtnCancle);
        mBtnSubmit=findViewById(R.id.mBtnSubmit);

        SurfaceHolder holder=surfaceView.getHolder();
        holder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {

            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

                mSurfaceHolder=holder;
                mCamera.startPreview();
                mCamera.cancelAutoFocus();
                mCamera.unlock();
            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {

            }
        });
    }
}
